package com.work.game.volley;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.android.volley.NetworkError;
import com.android.volley.ParseError;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.work.game.R;
import com.work.game.home.ui.enums.RedisEnum;
import com.work.game.utils.ViewUtils;
import com.work.game.view.LoadingDailog;
import com.work.game.volley.httpUtil.HCUParame;
import com.work.game.volley.httpUtil.HUCRequest;
import com.work.game.volley.httpUtil.interfase.HUCCallBackInterfase;
import com.work.game.volley.httpUtil.interfase.HUCParameInterfase;
import com.work.game.volley.httpUtil.interfase.HUCRequestInterfase;
import com.jiafan.xcwncw.wxapi.WXEntryActivity;

import java.util.Objects;

public class BaseFragment extends Fragment implements View.OnClickListener,
        HUCCallBackInterfase {
    // 网络请求接口类
    public HUCRequestInterfase hucRequestInterfase;
    // 网络请求构参接口类
    public HUCParameInterfase hucParameInterfase;
    public LoadingDailog loadingDailog;


    //声明SharedPreferences，用来读取xml;
    private SharedPreferences sp;
    //声明SharedPreferences.Editor，用来修改xml里面的值。
    private SharedPreferences.Editor ed;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initUtils();
        initView();
        initClick();
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 初始化当前视图
     */
    public void initView() {
        sp = Objects.requireNonNull(getActivity()).getSharedPreferences("setting", getActivity().MODE_PRIVATE);
    }

    /**
     * 绑定监听
     */
    public void initClick() {

    }

    /**
     * 初始化工具类
     */
    public void initUtils() {
        hucRequestInterfase = HUCRequest.getInstance();
        hucParameInterfase = HCUParame.getInstance();
    }

    public void sendIntent(Activity activity, Class myClass) {
        Intent intent = new Intent();
        intent.setClass(activity, myClass);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
    }

    public void sendIntent(Activity activity, Class myClass, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(activity, myClass);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivityForResult(intent, requestCode);
    }

    public void sendIntent(Activity activity, Class myClass, String value) {
        Intent intent = new Intent();
        intent.setClass(activity, myClass);
        intent.putExtra("tag", value);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
    }

    @Override
    public void onResetLogin(Object response) {
        Objects.requireNonNull(this.getActivity()).finish();
        sendIntent(this.getActivity(), WXEntryActivity.class);
    }

    public void showToast(String toastContent) {
        Toast.makeText(this.getActivity(), toastContent, Toast.LENGTH_SHORT).show();
    }

    public void loading() {
        LoadingDailog.Builder builder1 = new LoadingDailog.Builder(this.getActivity())
                .setMessage("加载中...")
                .setCancelOutside(true)
                .setCancelable(true);
        if (loadingDailog == null) {
            loadingDailog = builder1.create();
            loadingDailog.setCancelable(false);
            loadingDailog.show();
        }
        loadingDailog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (loadingDailog != null) {
                    hucRequestInterfase.cancelAll();
                    loadingDailog.dismiss();
                }
                loadingDailog = null;
            }
        });
    }

    public void loadDismiss() {
        if (loadingDailog != null) {
            loadingDailog.dismiss();
        }
        loadingDailog = null;
    }

    public AlertDialog showDialogLoading() {
        //判断提现账号
        AlertDialog.Builder dialog = new AlertDialog.Builder(this.getActivity());
        View root = LayoutInflater.from(this.getActivity()).inflate(R.layout.dialog_loading, null);
        dialog.setView(root);
        AlertDialog alertDialog = dialog.create();
        alertDialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
        alertDialog.setCancelable(false);
        alertDialog.show();
        return alertDialog;
    }

    public void setSharedPreferences(String key, String vlaue) {
        ed = sp.edit();
        ed.putString(key, vlaue);//保存token
        ed.commit();
    }

    public String getSharedPreferences(String key) {
        sp = Objects.requireNonNull(getActivity()).getSharedPreferences("setting", getActivity().MODE_PRIVATE);
        return sp.getString(key, "");
    }

    public String getSharedPreferences(String key,String mr) {
        sp = Objects.requireNonNull(getActivity()).getSharedPreferences("setting", getActivity().MODE_PRIVATE);
        return sp.getString(key, mr);
    }
    /**
     * 系统剪贴板-复制:   为内容
     *
     * @param context
     * @param s
     */
    public void copy(Context context, String s) {
        // 获取系统剪贴板
        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建一个剪贴数据集，包含一个普通文本数据条目（需要复制的数据）
        ClipData clipData = ClipData.newPlainText(null, s);
        // 把数据集设置（复制）到剪贴板
        clipboard.setPrimaryClip(clipData);
        Toast.makeText(context, "复制成功", Toast.LENGTH_SHORT).show();
    }

    /**
     * 点击事件
     */
    public void onClick(View v) {
    }

    public void onSuccessfulBitampType(int inputType, Bitmap response) {
        // TODO Auto-generated method stub

    }

    public void onErrorResponse(int inputType, VolleyError error) {
        // TODO Auto-generated method stub
        showToast("网络不稳定请重试");
        if (error instanceof TimeoutError) {
            // 处理超时错误
            Log.d("VolleyError", "处理超时错误");
        } else if (error instanceof NetworkError) {
            // 处理网络错误
            Log.d("VolleyError", "处理网络错误");
        } else if (error instanceof ServerError) {
            // 处理服务器错误
            Log.d("VolleyError", "处理服务器错误");
        } else if (error instanceof ParseError) {
            // 处理解析错误
            Log.d("VolleyError", "处理解析错误");
        }
    }

    @Override
    public void onErrorLogin(int inputType, Object response) {
        loadDismiss();
        ViewUtils.showRisk(this.getActivity(), response.toString(), getSharedPreferences(RedisEnum.APP_QQ_CUSTOMER_SERVICE.getKey()));
    }

    public void onSuccessfulDB(int inputType, Object obj) {
        // TODO Auto-generated method stub
    }

    public void onSuccessfulStrType(int inputType, Object response) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFailStrType(int inputType, Object response) {
        loadDismiss();
        Toast.makeText(this.getActivity(), response.toString(), Toast.LENGTH_LONG).show();
    }

}
